#include "kernel_operator.h"
using namespace AscendC;
constexpr int32_t BUFFER_NUM = 2;

class KernelAsinh {
public:
    __aicore__ inline KernelAsinh() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, uint32_t totalLen, uint32_t bufferSize, uint32_t tiles, uint32_t tailLen )
    {
        //考生补充初始化代码
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");

        this->bufferSize = bufferSize;
        this->totalLen = totalLen;
        this->tiles = tiles;
        this->tailLen = tailLen;
        xGm.SetGlobalBuffer((__gm__ DTYPE_X *)x, this->totalLen);
        yGm.SetGlobalBuffer((__gm__ DTYPE_Y *)y, this->totalLen);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->bufferSize * sizeof(DTYPE_X));
        pipe.InitBuffer(outQueueY, BUFFER_NUM, this->bufferSize * sizeof(DTYPE_Y));
        pipe.InitBuffer(xBuf, this->bufferSize*sizeof(float));
        pipe.InitBuffer(yBuf, this->bufferSize*sizeof(float));
        pipe.InitBuffer(absBuf, this->bufferSize*sizeof(float));
        pipe.InitBuffer(maskBuf, this->bufferSize*sizeof(float));
        pipe.InitBuffer(zeroBuf, this->bufferSize*sizeof(float));
        LocalTensor<DTYPE_Y> zerobuf = zeroBuf.Get<DTYPE_Y>();
        Duplicate(zerobuf, static_cast<DTYPE_Y>(0), bufferSize);
    }
    __aicore__ inline void Process()
    {
        int32_t pos = 0;
        if constexpr (std::is_same_v<DTYPE_X, half>){
            for(int i=0;i<this->tiles;i++){
                CopyIn(pos, bufferSize);
                Compute16(bufferSize);
                CopyOut(pos, bufferSize);
                pos+=this->bufferSize;
            }
            CopyIn(pos, tailLen);
            Compute16(bufferSize);
            CopyOut(pos, tailLen);
        }
        else{
            for(int i=0;i<this->tiles;i++){
                CopyIn(pos, bufferSize);
                Compute(bufferSize);
                CopyOut(pos, bufferSize);
                pos+=this->bufferSize;
            }
            CopyIn(pos, tailLen);
            Compute(bufferSize);
            CopyOut(pos, tailLen);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t pos, int32_t len)
    {
        LocalTensor<DTYPE_X> xLocal = inQueueX.AllocTensor<DTYPE_X>();
        DataCopy(xLocal, xGm[pos], len);
        inQueueX.EnQue(xLocal);
    }
    __aicore__ inline void Compute16(int32_t len)
    {
        LocalTensor<DTYPE_X> xsrc = inQueueX.DeQue<DTYPE_X>();
        LocalTensor<DTYPE_Y> ysrc = outQueueY.AllocTensor<DTYPE_Y>();
        LocalTensor<float> xLocal = xBuf.Get<float>();
        LocalTensor<float> yLocal = yBuf.Get<float>();

        Cast(xLocal, xsrc, RoundMode::CAST_NONE, len);
        
        Mul(yLocal, xLocal, xLocal, len);
        Adds(yLocal, yLocal, static_cast<float>(1), len);
        Sqrt(yLocal, yLocal, len);
        Add(yLocal, xLocal, yLocal, len);
        Ln(yLocal, yLocal, len);

        Cast(ysrc, yLocal, RoundMode::CAST_ROUND, len);

        outQueueY.EnQue<DTYPE_Y>(ysrc);
        inQueueX.FreeTensor(xsrc);
    }
    __aicore__ inline void Compute(int32_t len)
    {
        LocalTensor<DTYPE_X> xLocal = inQueueX.DeQue<DTYPE_X>();
        LocalTensor<DTYPE_Y> yLocal = outQueueY.AllocTensor<DTYPE_Y>();

        LocalTensor<DTYPE_Y> absbuf = absBuf.Get<DTYPE_Y>();
        LocalTensor<DTYPE_Y> zerobuf = zeroBuf.Get<DTYPE_Y>();
        LocalTensor<uint8_t> maskbuf = maskBuf.Get<uint8_t>();
        
        Compare(maskbuf, xLocal, zerobuf, CMPMODE::GE, len); //x>=0
        Abs(absbuf, xLocal, len);
        Mul(yLocal, xLocal, xLocal, len);
        Adds(yLocal, yLocal, static_cast<DTYPE_Y>(1), len);
        Sqrt(yLocal, yLocal, len);
        Add(yLocal, absbuf, yLocal, len);
        Ln(yLocal, yLocal, len);
        Sub(absbuf, zerobuf, yLocal, len); //absbuf = -ln(|x|+sqrt(x^2-1))
        Select(yLocal, maskbuf, yLocal, absbuf, SELMODE::VSEL_TENSOR_TENSOR_MODE, len);

        outQueueY.EnQue<DTYPE_Y>(yLocal);
        inQueueX.FreeTensor(xLocal);
    }
    __aicore__ inline void CopyOut(int32_t pos, int32_t len)
    {
        LocalTensor<DTYPE_Y> yLocal = outQueueY.DeQue<DTYPE_Y>();
        DataCopy(yGm[pos], yLocal, len);
        outQueueY.FreeTensor(yLocal);
    }

private:
    TPipe pipe;
    //create queue for input, in this case depth is equal to buffer num
    TQue<QuePosition::VECIN, BUFFER_NUM> inQueueX;
    //create queue for output, in this case depth is equal to buffer num
    TQue<QuePosition::VECOUT, BUFFER_NUM> outQueueY;
    TBuf<TPosition::VECCALC> xBuf, yBuf, absBuf, maskBuf, zeroBuf;
    GlobalTensor<DTYPE_X> xGm;
    GlobalTensor<DTYPE_Y> yGm;
    LocalTensor<uint8_t> mask;
    uint32_t totalLen, bufferSize, tiles, tailLen;

};



extern "C" __global__ __aicore__ void asinh(GM_ADDR x, GM_ADDR y, GM_ADDR workspace, GM_ADDR tiling) {
    GET_TILING_DATA(tiling_data, tiling);
    KernelAsinh op;
    op.Init(x,y,tiling_data.totalLen, tiling_data.bufferSize, tiling_data.tiles, tiling_data.tailLen);
    op.Process();
}